﻿namespace EyeOpen.Data.EntityFramework
{
	using System;
	using System.Collections;
	using System.Collections.Generic;
	using System.Data.Entity;
	using System.Linq;
	using System.Linq.Expressions;

	using EyeOpen.Domain;

	internal class Repository<T>
		: IRepository<T>
		where T : class, IEntity
	{
		private readonly IDbSet<T> set;
		private readonly IQueryable<T> queryable;

		public Repository(IDbSet<T> set)
		{
			this.set = set;
			queryable = set.AsQueryable();
		}

		public int Count
		{
			get
			{
				return queryable.Count();
			}
		}

		public virtual bool IsReadOnly
		{
			get
			{
				return false;
			}
		}

		public Expression Expression
		{
			get
			{
				return
					queryable.Expression;
			}
		}

		public Type ElementType
		{
			get
			{
				return
					queryable.ElementType;
			}
		}

		public IQueryProvider Provider
		{
			get
			{
				return
					queryable.Provider;
			}
		}

		public void Add(T entity)
		{
			set.Add(entity);
		}

		/// <exception cref="NotImplementedException"></exception>
		public void Update(T entity)
		{
			throw new NotImplementedException();
		}

		public bool Remove(T entity)
		{
			set.Remove(entity);
			return true;
		}

		public IEnumerator<T> GetEnumerator()
		{
			return
				queryable.GetEnumerator();
		}

		IEnumerator IEnumerable.GetEnumerator()
		{
			return GetEnumerator();
		}

		/// <exception cref="NotSupportedException"></exception>
		public void Clear()
		{
			throw new NotSupportedException();
		}

		/// <exception cref="NotSupportedException"></exception>
		public virtual bool Contains(T item)
		{
			throw new NotSupportedException();
		}

		/// <exception cref="NotImplementedException"></exception>
		public void CopyTo(T[] array, int arrayIndex)
		{
			throw new NotImplementedException();
		}
	}
}